package com.heima.controller.web.region;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.controller.Result;
import com.heima.controller.exception.BusinessException;
import com.heima.pojo.*;
import com.heima.pojo.vo.Dto.NodeDto;
import com.heima.pojo.vo.Dto.RegionDto;
import com.heima.pojo.vo.Dto.VendingMachineDto;
import com.heima.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/api/vm-service/node")
public class NodeController {

    @Autowired
    private RegionService regionService;
    @Autowired
    private BusinessService businessService;

    @Autowired
    private NodeService nodeService;

    @Autowired
    private VendingMachineService vendingMachineService;
    @Autowired
    private VmTypeService vmTypeService;


    /**
     * 分页查询
     * 查询区域详情信息
     *
     * @return
     */
    @GetMapping("/search")
    public Result<List<NodeDto>> findByPage(Integer pageIndex, Integer pageSize, String name, String regionId) {
        //健壮性
        log.info("区域管理，第{}页，每页{}条", pageIndex, pageSize);
        //        //健壮性
        if (pageSize == null) {
            pageSize = 10;
        }
        if (pageIndex == null) {
            pageIndex = 1;
        }
        //根基id 查询个数 并且进行分页 查询
        //创建分页对象
        Page<Node> page = new Page<>(pageIndex, pageSize);
        //创建分页条件
        LambdaQueryWrapper<Node> qw = new LambdaQueryWrapper<>();
        //如果有名字继续模糊查询
        if (name != null) {
            //模糊查询
            qw.like(Node::getName, name);
            //如果id id 添加区域id的条件
            if (regionId != null) {
                qw.eq(Node::getRegionId, regionId);
            }
            //进行分页查询
        }
        //如果没有名字
        else {
            //有id
            if (regionId != null) {
                qw.eq(Node::getRegionId, regionId);
            }
        }
        //模糊查询
        Page<Node> pageNode = nodeService.page(page, qw);
        //新建一个新的查询对象
        Page<NodeDto> pageNodeVo = new Page<NodeDto>(pageIndex, pageSize);
        //进行基本的赋值
        BeanUtils.copyProperties(pageNode, pageNodeVo, "records");
        //获取原来的数据
        List<Node> records = page.getRecords();
        List<NodeDto> nodeDtos = records.stream().map((record) -> {

            NodeDto nodeDto = new NodeDto();
            //复制基本信息
            BeanUtils.copyProperties(record, nodeDto);
            //添加其他没有的属性
            nodeDto.setArea(null);
            //根据 region_id来进行查询
            LambdaQueryWrapper<Region> qwNode = new LambdaQueryWrapper<>();
            qwNode.eq(Region::getId, record.getRegionId());
            //设置回去
            nodeDto.setRegion(regionService.getOne(qwNode));
            //根据 Business来进行查询
            LambdaQueryWrapper<Business> qwBusiness = new LambdaQueryWrapper<>();
            qwBusiness.eq(Business::getId, record.getBusinessId());
            nodeDto.setBusinessType(businessService.getOne(qwBusiness));
            //设置回去 查询 机器

            LambdaQueryWrapper<VendingMachine> qwVendingMachine = new LambdaQueryWrapper<>();
            qwVendingMachine.eq(VendingMachine::getNodeId, record.getId());
            nodeDto.setVmCount(vendingMachineService.count(qwVendingMachine));
            return nodeDto;
        }).collect(Collectors.toList());
        return Result.success(nodeDtos);
    }

    /**
     * 添加商圈
     *
     * @return
     */
    @PostMapping
    public boolean addNode(@RequestBody Node node) {
        if (node == null) {
            throw new BusinessException("你给的参数有问题");
        }
        //健壮性 名字不能一样哦
        LambdaQueryWrapper<Node> qw = new LambdaQueryWrapper<>();
        qw.eq(node.getName() != null, Node::getName, node.getName());
        int count = nodeService.count(qw);
        if (count == 0) {
            //保存
            boolean save = nodeService.save(node);
            if (save) {
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 查看点位详情
     */
    @GetMapping("/vmList/{id}")
    public List<VendingMachineDto> viewPoinit(@PathVariable Integer id) {
        //根据 node 的 id 进行查询  查询出有多少个机器 然后 去查询这些机器所在的位置

        LambdaQueryWrapper<VendingMachine> qw = new LambdaQueryWrapper<>();
        qw.eq(VendingMachine::getNodeId, id);
        List<VendingMachine> lists = vendingMachineService.list(qw);


        List<VendingMachineDto> newLists = lists.stream().map((list) -> {

            VendingMachineDto vmd = new VendingMachineDto();
            //进行基本数据的添加
            BeanUtils.copyProperties(list,vmd);
            //进行没有数据的填充
            //查询type 并进行设置
            LambdaQueryWrapper<VmType> vmTypqw = new LambdaQueryWrapper<>();
            vmTypqw.eq(VmType::getTypeId,list.getVmType());
            vmd.setType(vmTypeService.getOne(vmTypqw));
            //查询nodeDto  其中 有region 和region   area  和 region

            NodeDto nodeDto = new NodeDto();
            //查询Region
            LambdaQueryWrapper<Node> qwNode = new LambdaQueryWrapper<>();
            qwNode.eq(Node::getId, list.getNodeId());
            Node myNode = nodeService.getOne(qwNode);
            //进行基本数据的添加
            BeanUtils.copyProperties(myNode,nodeDto);
            //填充nodeDto
            //查询Region
            LambdaQueryWrapper<Region> qwRegion = new LambdaQueryWrapper<>();
            qwRegion.eq(Region::getId, list.getRegionId());
            //设置回去
            //查询regionResult
            Region regionResult = regionService.getOne(qwRegion);
            nodeDto.setRegion(regionResult);
            //根据 Business来进行查询
            LambdaQueryWrapper<Business> qwBusiness = new LambdaQueryWrapper<>();
            qwBusiness.eq(Business::getId, list.getBusinessId());
            nodeDto.setBusinessType(businessService.getOne(qwBusiness));
            //设置回去 查询 机器
            LambdaQueryWrapper<VendingMachine> qwVendingMachine = new LambdaQueryWrapper<>();
            qwVendingMachine.eq(VendingMachine::getNodeId, list.getId());
            nodeDto.setVmCount(vendingMachineService.count(qwVendingMachine));
            nodeDto.setArea(null);
            vmd.setNode(nodeDto);
            //查询regionDto
            RegionDto regionDto = new RegionDto();
            BeanUtils.copyProperties(regionResult,regionDto);
            //获取Dto没有的数据 获取count
            int count = nodeService.count(qwNode);
            System.out.println(count);
            regionDto.setNodeCount(count);
            System.out.println(regionDto.getNodeCount());
            vmd.setRegion(regionDto);
            //设置 fault
            vmd.setFault(null);
            //设置 status
            vmd.setStatus(null);
            return vmd;
        }).collect(Collectors.toList());
        return newLists;
    }


}
